package com.hzhh123;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;

import org.junit.Test;
/**
 * @author hzhh123
 * java8特性
 * Lambda表达式（匿名内部类）
 *
 */
public class TestLambda {

	/**
	 * 匿名内部类
	 */
	@org.junit.Test
	public void test1() {
		Comparator<Integer>comparator=new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return Integer.compare(o1, o2);
			}
		};
		
		TreeSet<Integer>treeSet=new TreeSet<>(comparator);
	}
	
	/**
	 * Lambda表达式
	 */
	@Test
	public void test2(){
		Comparator<Integer>comparator=(o1,o2)->Integer.compare(o1, o2);
		TreeSet<Integer>treeSet=new TreeSet<>(comparator);
	}

	List<Employee>employees=Arrays.asList(
			new Employee("张三",16,23898.00),
			new Employee("李四",38,12898.00),
			new Employee("王五",48,3898.00)
			);
	
	//需求1:查询年龄少于18岁的员工
	public List<Employee>fiterEmployeesByAge(List<Employee>employees){
		List<Employee>emps=new ArrayList<Employee>();
		for (Employee employee : employees) {
			if(employee.getAge()<18){
				emps.add(employee);
			}
		}
		return emps;
	}
	//需求2:工资大于4000
	public List<Employee>fiterEmployeesBySalary(List<Employee>employees){
		List<Employee>emps=new ArrayList<Employee>();
		for (Employee employee : employees) {
			if(employee.getSalary()>4000){
				emps.add(employee);
			}
		}
		return emps;
	}
	@Test
	public void test3(){
		System.out.println("年龄小于18岁");
		List<Employee> emps = fiterEmployeesByAge(employees);
		for (Employee employee : emps) {
			System.out.println(employee);
		}
		System.out.println("--------------------------------");
		System.out.println("工资大于4000");
		 emps = fiterEmployeesBySalary(employees);
		for (Employee employee : emps) {
			System.out.println(employee);
		}
	}
	
	
	/**
	 * 使用策略模式：接口编程
	 */
	/**
	 * 接口编程
	 * @param employees
	 * @param emp
	 * @return
	 */
	public List<Employee>fiterEmployees(List<Employee>employees,MyFilter<Employee>emp){
		List<Employee>emps=new ArrayList<Employee>();
		for (Employee employee : employees) {
			if(emp.test(employee)){
				emps.add(employee);
			}
		}
		return emps;
	}
	@Test
	public void test4(){
		List<Employee> emps = fiterEmployees(employees,new MyFilterByAge());
		for (Employee employee : emps) {
			System.out.println(employee);
		}
		System.out.println("--------------------");
		emps=fiterEmployees(employees,new MyFilterBySalary());
		for (Employee employee : emps) {
			System.out.println(employee);
		}
	}
	
	//使用匿名内部类
	@Test
	public void test5(){
		List<Employee> emps = fiterEmployees(employees,new MyFilter<Employee>(){

			@Override
			public boolean test(Employee t) {
				return t.getAge()<18;
			}
		});
		for (Employee employee : emps) {
			System.out.println(employee);
		}
		System.out.println("------------");
		emps = fiterEmployees(employees,new MyFilter<Employee>(){

			@Override
			public boolean test(Employee t) {
				return t.getSalary()>4000;
			}
		});
		for (Employee employee : emps) {
			System.out.println(employee);
		}
	}
	
	//使用Lambda表达式
	@Test
	public void test6(){
		List<Employee> emps=fiterEmployees(employees, (e)->e.getAge()<18);
		for (Employee employee : emps) {
			System.out.println(employee);
		}
		System.out.println("------------");
		emps=fiterEmployees(employees, (e)->e.getSalary()>4000);
		for (Employee employee : emps) {
			System.out.println(employee);
		}
	}
	
	//使用Stream API
	@Test
	public void test7(){
		employees.stream()
		.filter((e)->e.getAge()<18)
		.forEach(System.out::println);
		System.out.println("--------------");
		employees.stream()
		.filter((e)->e.getSalary()>4000)
		.limit(2)
		.forEach(System.out::println);
		
		System.out.println("-------------");
		System.out.println("遍历某个字段");
		employees.stream()
		.map(Employee::getName)
		.forEach(System.out::println);
	}
}
